package com.qmx.launcher.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.TextView;

import com.qmx.launcher.R;

public class CountdownActivity extends Activity {
    private TextView tv1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_countdown);
        tv1 = findViewById(R.id.textView1);
        handler.post(waitsendsrunnable);
    }

    //启用一个handler

    private Handler handler = new Handler() {

        public void handlemessage(Message msg) {
            switch (msg.what) {
                case 0:
//                    Intent intent = new Intent(this, mainactivity2.class);
//                    startActivity(intent);
                    break;
                case 1:
                    tv1.setText("倒计时：" + index + "s");
                    break;
                default:
                    break;
            }
        }
    };

// 倒计时五秒

    int index = 5;

    Runnable waitsendsrunnable = new Runnable() {
        public void run() {
            if (index > 0) {
                index--;
                try {
                    Thread.sleep(1000);
                    handler.sendEmptyMessage(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                handler.post(waitsendsrunnable);
            } else {
                try {
                    Thread.sleep(1000);
                    handler.sendEmptyMessage(0);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }

    };
}
